var ztree;
$(document).ready(function() {
    $("#pname").click(openTree);
    //监听表单提交
    layui.form.on('submit(submitBtn)', function(data){
        $.post('/ins/dept/update', $("#dataForm").serializeObject(), function(result) {
            if (200 == result.code) {
                close();
                parent.layui.table.reload('allInfoTable');
            } else {
                layer.alert("保存失败");
            }
        });
        return false;
    });

    var setting = {
        view: {
            dblClickExpand: false,
            showLine: true,
            selectedMulti: false
        },
        data: {
            key: {
                name: 'depName',
                url: 'xurl'
            },
            simpleData: {
                enable: true,
                idKey: 'id',
                pIdKey: 'pid',
                rootPId: 0
            },
        }
    };

    $.get('/ins/dept/findAll?id=' + $("#pid").val(), function(result) {
        ztree = $.fn.zTree.init($("#ztree"), setting, result.data);
        ztree.expandAll(true);
    });
});
/**
 * 关闭当前窗口
 */
function close() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}
function openTree() {
    layer.open({
        type: 1,
        title: '选择父级权限',
        fix: false,
        resize: false,
        area: ['300px', '290px'],
        content: $("#open_tree"),
        btn: ['确定', '关闭'],
        yes: function(index, layero) {
            var node = ztree.getSelectedNodes();
            var level=node[0].level;
            if(level==0){
                layer.alert("请选择子级菜单！")
                return;
            }
            //选择上级菜单
            $("#pid").val(node[0].id);
            $("#pname").val(node[0].depName);
            layer.close(index);
        },cancel: function (index, layero) {//取消事件
            layer.close(index);
        },
        end: function () {//无论是确认还是取消，只要层被销毁了，end都会执行，不携带任何参数。layer.open关闭事件
            $("#open_tree").hide();
        }
    });
}